package com.lz.sports.ui.checkmodel

import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.helper.idcard.FingerHelper
import com.lz.helper.idcard.FingerRequest
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.library.extension.showToast
import com.lz.sports.LogUtils
import com.lz.sports.R
import com.lz.sports.core.SoundHelper
import com.lz.sports.databinding.DialogFingerBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//指纹对比
class FingerDialog : BaseDialogFragment<DialogFingerBinding>(), FingerHelper.FingerCallback {

    companion object {
        //成功时的延时关闭
        private const val DELAY_CLOSE = 1000L

        fun show(activity: FragmentActivity, matcher: ByteArray, threshold: Float): FingerDialog {
            val dialog = FingerDialog().also {
                it.arguments = Bundle().apply {
                    putByteArray("matcher", matcher)
                    putFloat("threshold", threshold)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(fragment: Fragment, matcher: ByteArray, threshold: Float): FingerDialog {
            val dialog = FingerDialog().also {
                it.arguments = Bundle().apply {
                    putByteArray("matcher", matcher)
                    putFloat("threshold", threshold)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }
    }

    //指纹处理类
    private lateinit var fingerHelper: FingerHelper

    var onResultListener: OnResultListener? = null

    //声音提示
    private val soundHelper by lazy { SoundHelper() }

    override fun getLayoutRes(): Int = R.layout.dialog_finger

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val matcher = arguments!!.getByteArray("matcher")!!
        val threshold = arguments!!.getFloat("threshold")
        fingerHelper = FingerHelper(requireContext(), matcher, threshold, this)
        fingerHelper.start()

        soundHelper.init(
            intArrayOf(
                com.lz.helper.R.raw.finger_read,
                com.lz.helper.R.raw.finger_again,
                com.lz.helper.R.raw.finger_error
            )
        )
    }

    override fun initData(v: View, savedState: Bundle?) {
        lifecycleScope.launch {
            arrayOf(binding.ivBack).click().collect { dismiss() }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomDialog(requireContext(), false)

    override fun onResume() {
        super.onResume()
        fingerHelper.restart()
    }

    override fun onStop() {
        super.onStop()
        fingerHelper.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        fingerHelper.quit()
    }

    override fun onFingerResult(threshold: Int) {
        LogUtils.i("onFingerResult = $threshold")
        binding.ivState.setImageLevel(1)
        lifecycleScope.launch {
            onResultListener?.onResult(threshold)
            delay(DELAY_CLOSE)
            dismiss()
        }
    }

    override fun onFingerError(error: Int) {
        LogUtils.i("onFingerError $error")
        when (error) {
            FingerRequest.OPEN_FAIL -> R.string.inspect_finger_err.showToast()
            FingerRequest.ERR_UN_PRESS -> {
                binding.ivState.setImageLevel(0)
            }
            FingerRequest.ERR_MATCHING -> {
                soundHelper.play(1)
                binding.ivState.setImageLevel(2)
            }
        }
    }

    override fun onFingerOpened() {
        soundHelper.play(0)
    }

    interface OnResultListener {

        //识别成功
        fun onResult(threshold: Int)
    }
}